package com.houpu.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import com.houpu.pojo.Sort;
import com.houpu.service.SortMapperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class SortController {
    @Autowired
    private SortMapperService sortMapperService;

    @RequestMapping(value = "sort_selectall", method = RequestMethod.GET)
    public Map<String, Object> getSortAll(int page, int limit, String name, String remark) {
        PageHelper.startPage(page, limit);
        Sort sort = new Sort();
        List<Sort> selectAll = sortMapperService.getSelectAll(sort);
        if (!StringUtils.isEmpty(name)) {
            sort.setS_name(name);
        }
        if (!StringUtil.isEmpty(remark)) {
            sort.setS_remark(remark);
        }
        System.out.println(selectAll);
        PageInfo<Sort> pageInfo = new PageInfo<>(selectAll, limit);
        HashMap<String, Object> result = new HashMap<>();
        result.put("code", 0);
        result.put("data", pageInfo.getList());
        result.put("count", pageInfo.getTotal());
        return result;
    }

    @RequestMapping(value = "sort_insert", method = RequestMethod.POST)
    public Map<String, Object> add(@RequestBody Sort sort) {
        System.out.println(sort);

        //insert
        sortMapperService.getInsert(sort);

        HashMap<String, Object> result = new HashMap<>();
        result.put("status", "0");
        result.put("msg", "执行成功");
        return result;
    }

    @RequestMapping(value = "sort_class", method = RequestMethod.GET)
    public Map<String, Object> get_Class(int page,int limit) {
      /*  Sort sort = new Sort();
        List<Sort> classs = sortMapperService.get_classs(sort);
        for (Sort s : classs){
            System.out.println(s.getS_name());
        }*/
        PageHelper.startPage(page, limit);
        Sort sort = new Sort();
        List<Sort> sorts = sortMapperService.get_classs(sort);
        PageInfo<Sort> pageInfo = new PageInfo<>(sorts, limit);
        List<Sort> list = pageInfo.getList();
        /*List<Map<String,Object>> listmap = new ArrayList<>();
        list.forEach(e->{
        Map<String,Object> map = new HashMap<>();
            map.put("s_name",e.getS_name());
            listmap.add(map);
        });*/
        HashMap<String, Object> result = new HashMap<>();
        result.put("code", 0);
        result.put("data", list);
        result.put("count", pageInfo.getTotal());
        return result;
    }

    @RequestMapping(value = "sort_delete", method = RequestMethod.POST)
    public Map<String, Object> delete(@RequestBody Sort sort) {
        //delete_id
        sortMapperService.getdelete_id(sort);
        HashMap<String, Object> result = new HashMap<>();
        result.put("status", "1");
        result.put("msg", "执行成功");
        return result;
    }

    @RequestMapping(value = "sort_out", method = RequestMethod.GET)
    public Map<String, Object> get_out_id(int page, int limit) {
        PageHelper.startPage(page, limit);
        Sort sort = new Sort();
        List<Sort> sorts = sortMapperService.getout_id(sort);
        System.out.println(sorts);
        PageInfo<Sort> pageInfo = new PageInfo<>(sorts, limit);
        HashMap<String, Object> result = new HashMap<>();
        result.put("code", 0);
        result.put("msg", "");
        result.put("data", pageInfo.getList());
        result.put("count", pageInfo.getTotal());
        return result;
    }

    @RequestMapping(value = "sort_update", method = RequestMethod.POST)
    public Map<String, Object> get_update(@RequestBody Sort sort) {
        //update_id
        sortMapperService.get_update(sort);
        HashMap<String, Object> result = new HashMap<>();
        result.put("status", "1");
        result.put("msg", "执行成功");
        return result;
    }

    @RequestMapping(value = "sort_DP", method = RequestMethod.GET)
    public Map<String, Object> get_SP(int page, int limit) {
        PageHelper.startPage(page, limit);
        List<Sort> selectAll = sortMapperService.getSP_id();
        System.out.println(selectAll);
        PageInfo<Sort> pageInfo = new PageInfo<>(selectAll, limit);
        HashMap<String, Object> result = new HashMap<>();
        //封装的时一个类
        List<Sort> list = pageInfo.getList();
        List<Map<String, Object>> list1 = new ArrayList<>();
        list.forEach(e -> {
            Map<String, Object> map = new HashMap<>();
            map.put("p_id", e.getListProduct().get(0).getP_id());
            map.put("p_name", e.getListProduct().get(0).getP_name());
            map.put("p_area", e.getListProduct().get(0).getP_area());
            map.put("p_ml", e.getListProduct().get(0).getP_ml());
            map.put("p_is", e.getListProduct().get(0).getP_is());
            map.put("s_name", e.getS_name());
            list1.add(map);
        });
        result.put("code", 0);
        result.put("data", list1);
        result.put("count", pageInfo.getTotal());
        return result;
    }
}
